package tests;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import sourceFiles.CentralComputer;
import Exceptions.XMLRidesParserException;
/**
 * @author LASO-Team
 *
 */
public class TestExample {
	static private String masterXMLFilePath; // this will be the path of the
												// master xml file to copy
	static private String copyXMLFilePath; // this will be the path of the copy
											// of the master xml file on which
											// the test should work
	private static final String IN_PROJECT_PATH_TO_TESTS = "\\src\\tests"; // the
																			// location
																			// of
																			// the
																			// test
																			// folder
																			// within
																			// the
																			// project

	@BeforeClass
	public static void setUpBeforeClass() throws Exception { // initiating the
																// files paths
																// according to
																// where the
																// project runs
		String currentPath = new java.io.File(".").getCanonicalPath();
		currentPath = currentPath + IN_PROJECT_PATH_TO_TESTS;
		masterXMLFilePath = currentPath + "\\TestsMaster.xml";
		copyXMLFilePath = currentPath + "\\results\\ResultsExample.xml"; // resulting
																			// copy
																			// should
																			// be
																			// located
																			// within
																			// src/tests/results
																			// folder
	}

	@Before
	public void setUp() throws IOException { // the actual coping of the master
												// file to a new copy
		copySourceFile();
	}

	private void copySourceFile() throws FileNotFoundException, IOException {
		InputStream inStream = null;
		OutputStream outStream = null;

		File afile = new File(masterXMLFilePath);
		File bfile = new File(copyXMLFilePath);

		inStream = new FileInputStream(afile);
		outStream = new FileOutputStream(bfile);

		byte[] buffer = new byte[1024];

		int length;
		// copy the file content in bytes
		while ((length = inStream.read(buffer)) > 0) {

			outStream.write(buffer, 0, length);
		}
		inStream.close();
		outStream.close();
	}

	@Test
	public void testExample() throws XMLRidesParserException {
		CentralComputer cc = new CentralComputer(copyXMLFilePath); // now all
																	// operations
																	// with the
																	// central
																	// computer
																	// will be
																	// read and
																	// written
																	// to the
																	// file
																	// located
																	// at
																	// copyXMLFilePath
	}

}
